﻿using System;
using ExtraClub.Domain;
using ExtraClub.Domain.Entities;
using SubSonic;
using System.Collections.Generic;
using ExtraClub.Domain.Framework;
using System.Reflection;
using System.Globalization;

namespace ExtraClub.Web.Controls.mailer_grid {
    public partial class module : BaseModule {
        protected List<VwMailJob> list = null;
        Dictionary<string, object> data = null;

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            Query q = VwMailJob.Query();
            q.ORDER_BY(VwMailJob.Columns.Created, "desc");
            string key = "mailer_grid_filter";

            data = Items.ContainsKey("data") ? (Dictionary<string, object>)Items["data"] : (Dictionary<string, object>)Session[key];

            if (data == null) {
                SetDefaults();
            }
            
            //SiteContext.Current.SetLanguage(Constants.FormField.Language.ToString());

            q.AddWhere(VwMailJob.Columns.Subject, Comparison.Like, "%" + data[VwMailJob.Columns.Subject.ToLower()].ToString().Trim().ToLower() + "%");
            object[] created = (object[])data[MailJob.Columns.Created.ToLower()];

            DateTime dateStart = DateTime.Parse(created[0].ToString(), CultureInfo.CreateSpecificCulture("fr"));
            DateTime dateEnd = DateTime.Parse(created[1].ToString(), CultureInfo.CreateSpecificCulture("fr"));
            q.BETWEEN_AND(VwMailJob.Columns.Created, dateStart, dateEnd.AddDays(1).AddMilliseconds(-1));

            Session[key] = data;
            VwMailJobCollection all = new VwMailJobCollection();
            all.LoadAndCloseReader( VwMailJob.FetchByQuery(q));
            list = all.GetList();    
        }
        private void SetDefaults() {
            data = new Dictionary<string, object>();
            data.Add(VwMailJob.Columns.Subject.ToLower(), "");
            data.Add(VwMailJob.Columns.Created.ToLower(), new object[] { 
                        DateTime.Now.AddMonths(-1).ToString("dd/MM/yyyy"), 
                        DateTime.Now.ToString("dd/MM/yyyy")
                    });
        }
    }
}